package com.asset.inventory.view

import android.Manifest
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import com.asset.inventory.BR
import com.asset.inventory.R
import com.asset.inventory.base.BaseActivity
import com.asset.inventory.base.BaseApp
import com.asset.inventory.constant.ConfigPath
import com.asset.inventory.databinding.ActivityLoginBinding
import com.asset.inventory.kt.hideSoftKeyboard
import com.asset.inventory.kt.startActivity
import com.asset.inventory.kt.toast
import com.asset.inventory.model.TaskBean
import com.asset.inventory.utils.PermissionsUtils
import com.asset.inventory.viewModel.LoginViewModel
import java.io.File

/**
 *    @author : sibb
 *    date   : 21-3-16
 *    desc   : 登录页面
 */
class LoginActivity: BaseActivity<LoginViewModel,ActivityLoginBinding>() {

    val permissions = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE)


    override fun getLayoutId(): Int = R.layout.activity_login

    override fun initVariableId(): Int = BR.loginViewModel

    override fun initViewModel(): Class<LoginViewModel> = LoginViewModel::class.java


    override fun initView() {

    }

    fun checkReadWriteSdPermission(){
        PermissionsUtils.getInstance().chekPermissions(this, permissions, object: PermissionsUtils.IPermissionsResult{
            override fun passPermissons() {
                //2.读取SD卡指定目录下的任务文件。如果存在，离线登录成功；如果不存在，离线登录失败，toast 提示用户
                showLoading()
                val file = File(ConfigPath.offlineFilePath)
                if(file.exists()){
                    BaseApp.instance.isOnlineModel = false
                    startActivity<AssetInventoryRecordActivity>(this@LoginActivity)
                    finish()
                }else{
                    closeLoading()
                    toast("离线任务不存在，无法登录")
                }

            }

            override fun forbitPermissons() {
                toast("权限申请被拒绝，无法登录")
            }
        })


    }

    fun offlineLogin(view: View){
        //1.检查读写权限
        checkReadWriteSdPermission()
    }
    fun login(view: View){
        mViewModel.login()
    }

    override fun initListener() {
        super.initListener()
        mViewModel.observerLoginStatus().observe(this, Observer {
            if(it){
                startActivity<AssetInventoryRecordActivity>(this)
                finish()
            }else{
                toast("登录失败")
            }

        })

        mViewModel.loadingLiveData().observe(this, Observer {
            if(it){
                showLoading()
            }else{
                closeLoading()
            }
        })


    }

    override fun onDestroy() {
        super.onDestroy()
        closeLoading()
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionsUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);

    }

    fun closeLoading(){
        binding.loading.visibility = View.GONE
        binding.avi.smoothToHide()
    }


    fun showLoading(){
        hideSoftKeyboard()
        binding.loading.visibility = View.VISIBLE
        binding.avi.smoothToShow()
    }




}